package com.lvmama.pangolin.producthandler;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.github.pagehelper.PageInfo;
import com.lvmama.pangolin.common.Constant;
import com.lvmama.pangolin.common.StringTools;
import com.lvmama.pangolin.conf.ConfigManager;
import com.lvmama.pangolin.entity.tn.TNTicketCommodityItem;
import com.lvmama.pangolin.entity.tn.TNTicketProductItem;
import com.lvmama.pangolin.tn.service.TNTicketCommodityItemsService;
import com.lvmama.pangolin.tn.service.TNTicketProductItemsService;
@Component("tnTicketProductParamHandel")
public class TNTicketProductParamHandel {
	/**
	* @author zhoutianqi
	* @className TNTicketProductParamHandel.java
	* @date 2017年6月6日 下午12:19:53
	* @description 
	*/
	private Logger logger = Logger.getLogger(getClass());
	@Autowired
	private ConfigManager configManager;
	@Autowired
	private TNTicketProductItemsService productItemsService;
	@Autowired
	private TNTicketCommodityItemsService commodityItemsService;
	@Value("#{'${product_page_size}'}")
	private int productPageSize;
	//所有打包票商品类型
	private String packageCommType = "";
	@Async
	public void executeProcess(){
		//查询的页数，默认查询第一页
		int page = 1;
		//总页数
		int totalPage = 0;
		//所有打包票商品类型
		try {
			packageCommType = configManager.getParamValue(Constant.TICKET_COMMODITY_STARDARD_TYPE_CONFIG, Constant.TICKET_COMMODITY_STARDARD_TYPE_CONFIG);
			List<TNTicketProductItem> productList = productItemsService.selectByLimitNum(page, productPageSize);
			//当第一页没数据，表为空。当然一般不可能
			if(productList==null){
				logger.info("##############  TN产品处理线程处理完毕：");
				return;
			}
			PageInfo<TNTicketProductItem> pageInfo = new PageInfo<TNTicketProductItem>(productList);
			totalPage = pageInfo.getPages();
			// handle the first page data
			handelProduct(productList);
			//如果只有一页，不再作处理
			if(totalPage==1){
				logger.info("##############  TN产品处理线程处理完毕：");
				return;
			}
			for(int i=2;i<=totalPage;i++){
				List<TNTicketProductItem> productList1 = productItemsService.selectByLimitNum(i, productPageSize);
				handelProduct(productList);
			}
		} catch (Exception e) {
			logger.error("TN产品处理线程异常："+e.getMessage());
		}
		logger.info("##############  TN产品处理线程处理完毕：");
	}
	
	/**
	 * 处理产品字段逻辑
	*/
	public void handelProduct(List<TNTicketProductItem> productList){
		for(TNTicketProductItem item : productList){
			//优惠商品个数
			int commPreferentialNum = 0;
			//打包商品个数
			int commPackageNum = 0;
			List<TNTicketCommodityItem> commList = commodityItemsService.selectByProdId(item.getProduct_id());
			//设置产品对应商品的数量
			item.setCommodity_num(commList.size());
			for(TNTicketCommodityItem commItem : commList){
				if(StringTools.isValid(commItem.getPreferential())){commPreferentialNum++;}
				if(packageCommType.contains(commItem.getCommodity_type())){commPackageNum++;}
			}
			item.setComm_preferential_num(commPreferentialNum);
			item.setComm_package_num(commPackageNum);
			productItemsService.updateByItem(item);
		}
	}
}
